/**
 * ------------------------------------------------------
 *  用户相关状态管理
 * ------------------------------------------------------
 *
 * Note: .
 */

import { setToken as _setToken,getToken,removeToken } from '@/utils'
import { getProfileAPI } from '@/apis/user'
import { create } from 'zustand'

const useStore = create((set) => ({
    token: getToken() || '',
    userInfo: {},
    setToken: (logintoken) => {
        set({ token : logintoken })
        _setToken(logintoken)
    },
    setUserInfo: async () => {
        const res = await getProfileAPI();
        set({ 
            userInfo : res.data
        })
    },
    clearUserInfo: () => set({ token: '', userInfo: {} }, removeToken()),
  }))

const { userInfo, setToken, setUserInfo, clearUserInfo } = useStore

export { userInfo, setToken, setUserInfo, clearUserInfo}

export default useStore